﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	FormIniz
//	scheda o form principale del programma
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Drawing;
using System.Data.SQLite;
using System.Windows.Forms;
using System.IO;

namespace Map_Fvg
{
    public partial class FormIniz : Form
    {
        public static string cartellaSezioni;
        public static string cartellaElementi;
        public static string quadroUnione5000;
        public static string quadroUnione;
        public static string initialDirectory;
        public static string cartellaSimb;
        public static string zipfile;
        public static string bitmapFolder;
        public static string cartellaGPS;
        public static string nameElement;
        public static string nameSect;
        public static string cartellaElementi_isolati;
        public static Boolean bVeneto;

        private int oldPosX1, oldPosY1, oldPosX2, oldPosY2, oldPosX3, oldPosY3, oldPosX4, oldPosY4, oldPosX5, oldPosY5;
        private int oldPosX6, oldPosY6, oldPosX7, oldPosY7;

        public FormIniz()
        {
            InitializeComponent();

            oldPosX1 = this.ClientSize.Width - button1.Location.X;
            oldPosY1 = this.ClientSize.Height - button1.Location.Y;
            oldPosX2 = this.ClientSize.Width - button2.Location.X;
            oldPosY2 = this.ClientSize.Height - button2.Location.Y;
            oldPosX3 = this.ClientSize.Width - button3.Location.X;
            oldPosY3 = this.ClientSize.Height - button3.Location.Y;
            oldPosX4 = this.ClientSize.Width - button4.Location.X;
            oldPosY4 = this.ClientSize.Height - button4.Location.Y;
            oldPosX5 = this.ClientSize.Width - button5.Location.X;
            oldPosY5 = this.ClientSize.Height - button5.Location.Y;
            oldPosX6 = this.ClientSize.Width - button6.Location.X;
            oldPosY6 = this.ClientSize.Height - button6.Location.Y;
            oldPosX7 = this.ClientSize.Width - button7.Location.X;
            oldPosY7 = this.ClientSize.Height - button7.Location.Y;

            this.WindowState = FormWindowState.Maximized;
        }

        private void FormIniz_Load(object sender, EventArgs e)
        {
            readFileINI();
            deleteFolderSHT();
            button7.Visible = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  importare da files shp
            //

            string path = quadroUnione5000;
            string newpath = string.Empty;
            string path1 = string.Empty;

            string[] fileEntries = Directory.GetFileSystemEntries(path, "*.txt");
            foreach (string fileName in fileEntries)
            {
                if (fileName.Contains("Quadro25000.jpg")) continue;
                File.Delete(fileName);
            }

            ElencoFiles.InitialDirectory = cartellaSezioni;
            ElencoFiles dlg = new ElencoFiles();
            if (dlg.ShowDialog() != DialogResult.OK) return;
            path1 = ElencoFiles.sezioneFound;
            newpath = string.Concat(path1, @"\shp\");
            if (!Directory.Exists(newpath))
            {
                Directory.CreateDirectory(newpath);
                string fileName1 = string.Empty;
                string[] fileEntries1 = Directory.GetFileSystemEntries(path1);
                bool ifound = false;
                foreach (string fileName in fileEntries1)
                {
                    if (fileName.Contains(".zip") || fileName.Contains(".ZIP"))
                    {
                        fileName1 = string.Copy(fileName);
                        ifound = true;
                        break;
                    }
                }
                if (!ifound)
                {
                    MessageBox.Show("non è presente il file .Zip");
                    return;
                }

                Directory.CreateDirectory(newpath);

                Zip.Extract1 Extr = new Zip.Extract1();

                int iret = Extr.ExtractToDirectory(fileName1, newpath);

                if (iret != 0)
                {
                    MessageBox.Show("impossibile eseguire il download");
                    return;
                }
            }

            int ipos = path1.LastIndexOf('\\');
            if (ipos >= 0)
            {
                int len = path1.Length;
                ++ipos;
                nameSect = path1.Substring(ipos, len - ipos);
            }

            fileEntries = Directory.GetFileSystemEntries(newpath, "*.dbf");
            convert cnv = new convert();
            foreach (string fileName in fileEntries)
            {
                //                MessageBox.Show(fileName);
                if (fileName.Contains("AREA")) cnv.convert_dbf1(fileName, "area");
                if (fileName.Contains("LINE")) cnv.convert_dbf1(fileName, "line");
                if (fileName.Contains("POINT")) cnv.convert_dbf1(fileName, "point");
                if (fileName.Contains("TEXT")) cnv.convert_dbf1(fileName, "text");
            }
            fileEntries = Directory.GetFileSystemEntries(newpath, "*.shp");
            foreach (string fileName in fileEntries)
            {
                //     MessageBox.Show(fileName);
                if (fileName.Contains("AREA")) cnv.convert_shp1(fileName, "area");
                if (fileName.Contains("LINE")) cnv.convert_shp1(fileName, "line");
                if (fileName.Contains("POINT")) cnv.convert_shp1(fileName, "point");
                if (fileName.Contains("TEXT")) cnv.convert_shp1(fileName, "text");

            }

            int iret2 = cnv.splitFiles(newpath);
            if (iret2 != 0)
            {
                MessageBox.Show("non è' possibile estrarre i files dell'elemento scelto");
                return;
            }
            bVeneto = false;
            ViewQ newMDIChild = new ViewQ();
            newMDIChild.MdiParent = Map_Fvg.MainWindow.ActiveForm;
            newMDIChild.Show();
            newMDIChild.WindowState = FormWindowState.Maximized;

            path = quadroUnione5000;
            int iret1 = newMDIChild.openView(path);
            if (iret1 == 0)
            {
                aggiornaSimboli();
                ViewQ.ActiveForm.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //  archivio locale
            //

            string path = quadroUnione5000;
            string newpath = string.Empty;

            string[] fileEntries = Directory.GetFileSystemEntries(path, "*.txt");
            foreach (string fileName in fileEntries)
            {
                if (fileName.Contains("Quadro25000.jpg")) continue;
                File.Delete(fileName);
            }

            ElencoFiles.InitialDirectory = cartellaElementi;
            ElencoFiles dlg = new ElencoFiles();
            if (dlg.ShowDialog() != DialogResult.OK) return;
            string ss = ElencoFiles.sezioneFound;

            int ipos = ss.LastIndexOf('\\');
            if (ipos >= 0)
            {
                int len = ss.Length;
                ++ipos;
                nameSect = ss.Substring(ipos, len - ipos);
            }

            ipos = ss.LastIndexOf('\\');
            if (ipos >= 0)
            {
                int len = ss.Length;
                ++ipos;
                nameElement = ss.Substring(ipos, len - ipos - 4);
            }

            ViewQ.noFilesSelected = 1;
            bVeneto = false;
            int iret = RestoreFiles(ss, -1);
            if (iret == 0)
            {
                ViewQ.scalaXY = 1.0F;
                ViewQ.iskip = 0;
                ViewQ newMDIChild = new ViewQ();
                newMDIChild.MdiParent = MainWindow.ActiveForm;

                path = quadroUnione5000;

                ViewQ.noFilesSelected = 1;

                iret = newMDIChild.openView(path);
                if (iret == 0)
                {
                    aggiornaSimboli();

                    for (int i = 0; i < ViewQ.nSimbP; ++i)
                    {
                        
                        if (ViewQ.vectP[i].codice.Contains("3P000AL")) ViewQ.vectP[i].visib = 0;    //  3P000AL ==  albero isolato
                    }

  //                  ViewQ.ActiveForm.Refresh();
  
                    newMDIChild.Show();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private static System.Timers.Timer aTimer;

        private void button4_Click(object sender, EventArgs e)
        {
            //  quadro unione
            //

            QuadroUnione3.dResult = 0;
            QuadroUnione3 newMDIChild = new QuadroUnione3();
            newMDIChild.MdiParent = Map_Fvg.MainWindow.ActiveForm;
            newMDIChild.Show();

            aTimer = new System.Timers.Timer(100);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnExtractTimer;
            aTimer.Enabled = true;

        }

        public void OnExtractTimer(object sender, EventArgs e)
        {
            // stop the timer
            aTimer.Stop();      //  ??????????????????

            if (QuadroUnione3.dResult == 0)
            {
                aTimer.Start();
                return;
            }
            aTimer.Dispose();
            aTimer = null;
            if (QuadroUnione3.dResult == DialogResult.OK)
            {
                MessageBox.Show("cliccare su \"importare da files shp\" e selezionare la sezione appena scaricata    (per il momento)");

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //  gestione simboli
            //
            InfoDialog3 dlg = new InfoDialog3();

            DialogResult dr = dlg.ShowDialog();
            if (dr == DialogResult.No) return;

            Simbol newMDIChild = new Simbol();
            newMDIChild.MdiParent = Map_Fvg.MainWindow.ActiveForm;
            newMDIChild.Show();
            newMDIChild.WindowState = FormWindowState.Maximized;
        }

        public static DataTable Settings;

        private void initializeDataTable() //re-initializes the table with the proper columns
        {
            Settings = new DataTable("Settings");

            Settings.Columns.Add("Category", typeof(string));
            Settings.Columns.Add("SettingKey", typeof(string));
            Settings.Columns.Add("SettingsValue", typeof(string));
        }

        static public void AddValue(string Category, string Key, string Value, bool OverwriteExisting) //adds a new setting to the table
        {
            DataRow newRow = Settings.NewRow();
            newRow["Category"] = Category.Trim();
            newRow["SettingKey"] = Key.Trim();
            newRow["SettingsValue"] = Value.Trim();
            Settings.Rows.Add(newRow);

        }

        public static string GetKey(string Category, int ndx) //gets a key 
        {
            int idx = 0;
            foreach (DataRow row in Settings.Rows)
            {
                string ss = (string)row[0];

                if (!ss.Contains(Category)) continue;
                if (idx != ndx)
                {
                    ++idx;
                    continue;
                }

                return (string)row[1];
            }
            return string.Empty;
        }

        public static int GetCount(string Category) //gets the count of items with a selected category
        {
            int count = 0;
            foreach (DataRow row in Settings.Rows)
            {
                string ss = (string)row[0];

                if (!ss.Contains(Category)) continue;
                ++count;
            }
            return count;
        }

        public static string GetValue(string Category, string Key, string DefaultValue) //gets a value or returns a default value
        {
            foreach (DataRow row in Settings.Rows)
            {
                string ss = (string)row[0];

                if (!ss.Contains(Category)) continue;
                string sr = (string)row[1];
                if (sr.Contains(Key))
                {
                    return (string)row[2];
                }
            }
            return DefaultValue;
        }

        public void readFileINI()
        {
            string path = Directory.GetCurrentDirectory();

            string fileName = "\\Map_Fvg.ini";
            fileName = string.Concat(path, fileName);
            if (!File.Exists(fileName))
            {
                MessageBox.Show("non esiste il file Map_Fvg.ini");
                return;
            }

            initializeDataTable();

            string currentCategory = ""; //holds the category we're at

            using (StreamReader sr = File.OpenText(fileName))
            {
                while (!sr.EndOfStream) //goes through the file
                {
                    string currentLine = sr.ReadLine(); //reads the current file

                    if (currentLine.Length < 3) continue; //checks that the line is usable

                    if (currentLine.StartsWith("[") && currentLine.EndsWith("]")) //checks if the line is a category marker
                    {
                        currentCategory = currentLine.Substring(1, currentLine.Length - 2);
                        continue;
                    }

                    if (!currentLine.Contains("=")) continue; //or an actual setting

                    string currentKey = currentLine.Substring(0, currentLine.IndexOf("=", StringComparison.Ordinal));

                    string currentValue = currentLine.Substring(currentLine.IndexOf("=", StringComparison.Ordinal) + 1);

                    AddValue(currentCategory, currentKey, currentValue, true);

                }

                sr.Close(); //closes the stream
            }
            int count = Settings.Rows.Count;
            //         if(count == 0)
            cartellaSezioni = GetValue("Directories", "cartellaSezioni", "default value");
            if (cartellaSezioni == "default value") MessageBox.Show("non trovata cartellaSezioni");
            //          if(!ExistDirect

            cartellaElementi = GetValue("Directories", "cartellaElementi", "default value");
            if (cartellaElementi == "default value") MessageBox.Show("non trovata cartellaElementi");

            quadroUnione5000 = GetValue("Directories", "quadroUnione5000", "default value");
            if (quadroUnione5000 == "default value") MessageBox.Show("non trovato quadroUnione5000");

            quadroUnione = GetValue("Directories", "quadroUnione", "default value");
            if (quadroUnione == "default value") MessageBox.Show("non trovato quadroUnione");

            cartellaSimb = GetValue("Directories", "dataSimb", "default value");
            if (cartellaSimb == "default value") MessageBox.Show("non trovato dataSimb");

            zipfile = GetValue("Directories", "zipfile", "default value");
            if (zipfile == "default value") MessageBox.Show("non trovato zipfile");
            zipfile = zipfile.Trim();

            bitmapFolder = GetValue("Directories", "bitmap", "default value");
            if (bitmapFolder == "default value") MessageBox.Show("non trovato bitmap");

            cartellaGPS = GetValue("Directories", "cartellaGPS", "default value");
            if (cartellaGPS == "default value") MessageBox.Show("non trovato cartellaGPS");

            cartellaElementi_isolati = GetValue("Directories", "cartellaElementi_isolati", "default value");
            if (cartellaElementi_isolati == "default value") MessageBox.Show("non trovato cartellaElementi_isolati");


            initialDirectory = Directory.GetCurrentDirectory();
        }

        static public int RestoreFiles(string fileName, int noFile)     //  3/1/2015
        {
            int iret = 0;
            bool bZip = fileName.Contains(".zip");
            if (bZip)
            {
                if (noFile < 0)
                {

                    Zip.Extract1 Extr = new Zip.Extract1();

                    iret = Extr.ExtractToDirectory(fileName, quadroUnione5000);

                    if (iret != 0)
                    {
                        MessageBox.Show("impossibile eseguire la decompressione");
                    }
                    //  aggiungere controllo versione    

                    return (iret);
                }
                else
                {
                    //
                    //  chiamato da OpenUnione()
                    //  aggiungere controllo versione
                    //
                    Zip.Extract1 Extr = new Zip.Extract1();

                    iret = Extr.ExtractToDirectory(fileName, quadroUnione5000);

                    if (iret == 0)
                    {
                        string OldName = quadroUnione5000 + "area_dbf.txt";
                        string NewName = quadroUnione5000 + "area_dbf" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "area_shp.txt";
                        NewName = quadroUnione5000 + "area_shp" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "line_dbf.txt";
                        NewName = quadroUnione5000 + "line_dbf" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "line_shp.txt";
                        NewName = quadroUnione5000 + "line_shp" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);


                        OldName = quadroUnione5000 + "point_dbf.txt";
                        NewName = quadroUnione5000 + "point_dbf" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "point_shp.txt";
                        NewName = quadroUnione5000 + "point_shp" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "text_dbf.txt";
                        NewName = quadroUnione5000 + "text_dbf" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);

                        OldName = quadroUnione5000 + "text_shp.txt";
                        NewName = quadroUnione5000 + "text_shp" + noFile.ToString() + ".txt";
                        if (File.Exists(NewName)) File.Delete(NewName);
                        File.Move(OldName, NewName);
                    }
                    else
                    {
                        MessageBox.Show("impossibile eseguire la decompressione");
                    }
                }
            }
            else iret = 1;
            return (iret);
        }


        public void aggiornaSimboli()
        {
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectS[i].selected == 0) continue;
                string ss = ViewQ.vectS[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from linea where codice_stclasse = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectS[i].penType = int.Parse(reader.GetString(3).ToString());
                                ViewQ.vectS[i].tratto = reader.GetString(4).ToString();

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                string s1 = reader.GetString(5).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectS[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                    }
                    conn.Close();
                }

            }

            for (int i = 0; i < ViewQ.nSimbT; ++i)
            {
                if (ViewQ.vectT[i].selected == 0) continue;
                string ss = ViewQ.vectT[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from testo where codice_testo = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectT[i].iFont = stringToFont(s1);

                                ViewQ.vectT[i].altezza = float.Parse(reader.GetString(4).ToString());

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                s1 = reader.GetString(3).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectT[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }


            for (int i = 0; i < ViewQ.nSimbA; ++i)
            {
                if (ViewQ.vectA[i].selected == 0) continue;
                string ss = ViewQ.vectA[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from area where codice_area = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectA[i].codice_bordo = int.Parse(reader.GetString(4).ToString());
                                ViewQ.vectA[i].bordo = reader.GetString(5).ToString();
                                ViewQ.vectA[i].codice_camp = int.Parse(reader.GetString(2).ToString());
                                ViewQ.vectA[i].campitura = reader.GetString(3).ToString();


                                s1 = reader.GetString(6).ToString();
                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectA[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }

            for (int i = 0; i < ViewQ.nSimbP; ++i)
            {
                //           if (ViewQ.vectP[i].selected == 0) continue;
                string ss = ViewQ.vectP[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from point where codice_point = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectP[i].simbolo = int.Parse(reader.GetString(2).ToString());

                                ViewQ.vectP[i].paramSimb = reader.GetString(3).ToString();

                                string s1 = reader.GetString(4).ToString();
                                ViewQ.vectP[i].fileName = s1.Trim();
                                ViewQ.vectP[i].dimensione = float.Parse(reader.GetString(5).ToString());
                                s1 = reader.GetString(6).ToString();
                            }
                        }
                        conn.Close();
                    }
                }
            }
        }


        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);
        }

        private void FormIniz_SizeChanged(object sender, EventArgs e)
        {

            Point pnt1 = new Point(this.ClientSize.Width - oldPosX1, this.ClientSize.Height - oldPosY1);
            button1.Location = pnt1;

            Point pnt2 = new Point(this.ClientSize.Width - oldPosX2, this.ClientSize.Height - oldPosY2);
            button2.Location = pnt2;

            Point pnt3 = new Point(this.ClientSize.Width - oldPosX3, this.ClientSize.Height - oldPosY3);
            button3.Location = pnt3;

            Point pnt4 = new Point(this.ClientSize.Width - oldPosX4, this.ClientSize.Height - oldPosY4);
            button4.Location = pnt4;

            Point pnt5 = new Point(this.ClientSize.Width - oldPosX5, this.ClientSize.Height - oldPosY5);
            button5.Location = pnt5;

            Point pnt6 = new Point(this.ClientSize.Width - oldPosX6, this.ClientSize.Height - oldPosY6);
            button6.Location = pnt6;

            Point pnt7 = new Point(this.ClientSize.Width - oldPosX7, this.ClientSize.Height - oldPosY7);
            button7.Location = pnt7;
        }

        private void deleteFolderSHT()
        {
            string[] fileEntries = Directory.GetDirectories(cartellaSezioni);

            //         MessageBox.Show("numero di cartelle  " + fileEntries.Length.ToString());
            foreach (string fileName in fileEntries)
            {
                string filex = fileName + @"\";
                string[] fileEntries1 = Directory.GetDirectories(filex, "shp");
                if (fileEntries1.Length > 0)
                {
                    string filey = fileName + @"\shp";
                    Directory.Delete(filey, true);
                }

            }
        }

        private void newViewQ()
        {

            Cursor.Current = Cursors.WaitCursor;
            string path1 = WebForm.pathFile;
            string newpath = string.Concat(path1, @"\shp\");
            if (!Directory.Exists(newpath))
            {
                Directory.CreateDirectory(newpath);
                string fileName1 = string.Empty;
                string[] fileEntries1 = Directory.GetFileSystemEntries(path1);
                bool ifound = false;
                foreach (string fileName in fileEntries1)
                {
                    if (fileName.Contains(".ZIP"))
                    {
                        fileName1 = string.Copy(fileName);
                        ifound = true;
                        break;
                    }
                }
                if (!ifound)
                {
                    MessageBox.Show("non è presente il file .Zip");
                    return;
                }

                Directory.CreateDirectory(newpath);

                Zip.Extract1 Extr = new Zip.Extract1();

                int iret = Extr.ExtractToDirectory(fileName1, newpath);

                if (iret != 0)
                {
                    MessageBox.Show("impossibile eseguire il download");
                    return;
                }
                else MessageBox.Show("terminata la decompressione");
            }


            string[] fileEntries3 = Directory.GetFileSystemEntries(newpath, "*.dbf");
            convert cnv = new convert();
            foreach (string fileName1 in fileEntries3)
            {
                if (fileName1.Contains("AREA")) cnv.convert_dbf1(fileName1, "area");
                if (fileName1.Contains("LINE")) cnv.convert_dbf1(fileName1, "line");
                if (fileName1.Contains("POINT")) cnv.convert_dbf1(fileName1, "point");
                if (fileName1.Contains("TEXT")) cnv.convert_dbf1(fileName1, "text");
            }
            string[] fileEntries2 = Directory.GetFileSystemEntries(newpath, "*.shp");
            foreach (string fileName2 in fileEntries2)
            {
                //     MessageBox.Show(fileName);
                if (fileName2.Contains("AREA")) cnv.convert_shp1(fileName2, "area");
                if (fileName2.Contains("LINE")) cnv.convert_shp1(fileName2, "line");
                if (fileName2.Contains("POINT")) cnv.convert_shp1(fileName2, "point");
                if (fileName2.Contains("TEXT")) cnv.convert_shp1(fileName2, "text");

            }
            Cursor.Current = Cursors.Cross;

            ViewQ newMDIChild = new ViewQ();
            //  http://msdn.microsoft.com/en-us/library/ms171728(VS.90).aspx

            newMDIChild.MdiParent = Map_Fvg.MainWindow.ActiveForm;
            newMDIChild.Show();
            newMDIChild.WindowState = FormWindowState.Maximized;

            string path = FormIniz.quadroUnione5000;

            int iret1 = newMDIChild.openView(path);
            if (iret1 == 0)
            {
                AggiornaSimboli agSim = new AggiornaSimboli();
                agSim.aggiornaSimboli1();
                ViewQ.ActiveForm.Refresh();
            }


            Cursor.Current = Cursors.Default;

            QuadroUnione3.dResult = 0;
        }

        private void button6_Click(object sender, EventArgs e)
        {

            // mappe isolate
            //

            string path = quadroUnione5000;
            string newpath = string.Empty;

            string[] fileEntries = Directory.GetFileSystemEntries(path, "*.txt");
            foreach (string fileName in fileEntries)
            {
   //             if (fileName.Contains("QU_5000.dat")) continue;
   //             if (fileName.Contains("QU_25000.txt")) continue;
                if (fileName.Contains("Quadro25000.jpg")) continue;
   //             if (fileName.Contains("versione_01")) continue;
                File.Delete(fileName);
            }

            ElencoFiles.InitialDirectory = cartellaElementi_isolati;
            ElencoFiles dlg = new ElencoFiles();
            if (dlg.ShowDialog() != DialogResult.OK) return;
            string ss = ElencoFiles.sezioneFound;

            int ipos = ss.LastIndexOf('\\');
            if (ipos >= 0)
            {
                int len = ss.Length;
                ++ipos;
                nameSect = ss.Substring(ipos, len - ipos);
            }
            ViewQ.noFilesSelected = 1;
            bVeneto = true;
            int iret = RestoreFiles(ss, -1);
            if (iret == 0)
            {
                ViewQ.scalaXY = 1.0F;
                ViewQ.iskip = 0;
                ViewQ newMDIChild = new ViewQ();
                newMDIChild.MdiParent = MainWindow.ActiveForm;
                newMDIChild.Show();
                newMDIChild.WindowState = FormWindowState.Maximized;
                path = quadroUnione5000;

                ViewQ.noFilesSelected = 1;

                iret = newMDIChild.openView(path);
                if (iret == 0)
                {
                    aggiornaSimboli();


                    for (int i = 0; i < ViewQ.nSimbP; ++i)
                    {
                        //  3P000AL ==  albero isolato
                        //                     if (ViewQ.vectP[i].codice.Contains("3P000AL")) ViewQ.vectP[i].visib = 0;
                    }

                    ViewQ.ActiveForm.Refresh();
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {

            // conversione veneta
            //

            string path = quadroUnione5000;
            string newpath = string.Empty;
            string path1 = string.Empty;

            string[] fileEntries = Directory.GetFileSystemEntries(path, "*.txt");
            foreach (string fileName in fileEntries)
            {
                //             if (fileName.Contains("QU_5000.dat")) continue;
                //           if (fileName.Contains("QU_25000.txt")) continue;
                if (fileName.Contains("Quadro25000.jpg")) continue;
      //          if (fileName.Contains("versione_01")) continue;
                File.Delete(fileName);
            }


            string cartellaVeneta = @"C:\Map_FVG_cartelle\Veneto\";

            this.Cursor = Cursors.WaitCursor;

            newpath = string.Concat(cartellaVeneta, @"\047012o\");
            fileEntries = Directory.GetFileSystemEntries(newpath, "*.dbf");
            convert cnv = new convert();
            /**/
            foreach (string fileName in fileEntries)
            {
                //                       MessageBox.Show(fileName);

                if (fileName.Contains("curve")) cnv.convert_dbf2(fileName, "curve");
                if (fileName.Contains("testi")) cnv.convert_dbf2(fileName, "testi");
                if (fileName.Contains("aree_ter")) cnv.convert_dbf2(fileName, "aree_ter");
                if (fileName.Contains("punti_q")) cnv.convert_dbf2(fileName, "punti_q");
                if (fileName.Contains("veget_a")) cnv.convert_dbf2(fileName, "veget_a");
                if (fileName.Contains("astevia")) cnv.convert_dbf2(fileName, "astevia");
                if (fileName.Contains("idrograf")) cnv.convert_dbf2(fileName, "idrograf");
                if (fileName.Contains("discont")) cnv.convert_dbf2(fileName, "discont");
                if (fileName.Contains("asteflu")) cnv.convert_dbf2(fileName, "asteflu");
                if (fileName.Contains("viabilit")) cnv.convert_dbf2(fileName, "viabilit");
                if (fileName.Contains("vegetaz")) cnv.convert_dbf2(fileName, "vegetaz");
            }
            int iret3 = 0;
            if (iret3 != 0)
            {
                MessageBox.Show("non è' possibile estrarre i files dell'elemento scelto");
                return;
            }
            fileEntries = Directory.GetFileSystemEntries(newpath, "*.shp");
            foreach (string fileName in fileEntries)
            {
                if (fileName.Contains("CURVE")) cnv.convert_shp3(fileName, "line");
                if (fileName.Contains("AREE_TER")) cnv.convert_shp3(fileName, "area");
                if (fileName.Contains("TESTI")) cnv.convert_shp3(fileName, "text");
                if (fileName.Contains("PUNTI_Q")) cnv.convert_shp3(fileName, "point");
                if (fileName.Contains("VEGET_A")) cnv.convert_shp3(fileName, "veget_a");
                if (fileName.Contains("ASTEVIA")) cnv.convert_shp3(fileName, "astevia");
                if (fileName.Contains("IDROGRAF")) cnv.convert_shp3(fileName, "idrograf");
                if (fileName.Contains("DISCONT")) cnv.convert_shp3(fileName, "discont");
                if (fileName.Contains("ASTEFLU")) cnv.convert_shp3(fileName, "asteflu");
                if (fileName.Contains("VIABILIT")) cnv.convert_shp3(fileName, "viabilit");
                if (fileName.Contains("VEGETAZ")) cnv.convert_shp3(fileName, "vegetaz");
            }
            iret3 = 0;
            if (iret3 != 0)
            {
                MessageBox.Show("non è' possibile estrarre i files dell'elemento scelto");
                return;
            }


            int iret2 = cnv.compactFiles();
            if (iret2 != 0)
            {
                MessageBox.Show("non è' possibile estrarre i files dell'elemento scelto");
                return;
            }

            MessageBox.Show("eseguita la conversione");
            this.Cursor = Cursors.Default;

            return;

        }





    }
}
